package com.gwkj.zxjl_user.AccountDB;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by fujh on 16/2/3.
 */
public class ADBHelper extends SQLiteOpenHelper {
    // 表名
    private static final String DB_NAME = "account.db";
    // 版本号
    private static final int VERSION = 1;
    // 建表语句
    private static final String CREATE_ACCOUNT = "create table account_info(_id integer primary key autoincrement,"
            + "worker_id integer, mobile text, session_id text)";

    private static final String DELETE_ACCOUNT = "DROP TABLE IF EXISTS account_info";

    private static final String CREATE_DEVICE = "create table device_info(_id integer primary key autoincrement," +
            "platform text, version text, app text, id text, info text)";

    private static final String DELETE_DEVICE = "DROP TABLE IF EXISTS device_info";

    private ADBHelper(Context context)
    {
        super(context, DB_NAME, null, VERSION);
    }

    private static ADBHelper sHelper = null;

    /**
     * 单例模式
     * @param context
     * @return
     */
    public static ADBHelper getInstanceDBHelper(Context context)
    {
        //提高效率
        if(sHelper == null)
        {
            //同步锁
            synchronized (ADBHelper.class)
            {
                if(sHelper == null)
                    sHelper = new ADBHelper(context);
            }
        }
        return sHelper;
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {
        // 建表
        db.execSQL(CREATE_ACCOUNT);
        db.execSQL(CREATE_DEVICE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        db.execSQL(DELETE_ACCOUNT);
        db.execSQL(DELETE_DEVICE);
        onCreate(db);
    }
}
